﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.Panasonic
{
	/// <summary>
	/// 松下PLC的数据交互协议，采用Mewtocol协议通讯，支持的地址列表参考api文档<br />
	/// The data exchange protocol of Panasonic PLC adopts Mewtocol protocol for communication. For the list of supported addresses, refer to the api document.
	/// </summary>
	/// <remarks>
	/// 地址支持携带站号的访问方式，例如：s=2;D100
	/// </remarks>
	/// <example>
	/// <inheritdoc cref="T:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp" path="example" />
	/// </example>
	// Token: 0x02000054 RID: 84
	public class PanasonicMewtocol : SerialDeviceBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.#ctor(System.Byte)" />
		// Token: 0x060006D7 RID: 1751 RVA: 0x00023E9F File Offset: 0x0002209F
		public PanasonicMewtocol(byte station = 238)
		{
			base.ByteTransform = new RegularByteTransform();
			this.Station = station;
			base.ByteTransform.DataFormat = DataFormat.DCBA;
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.Station" />
		// Token: 0x1700014B RID: 331
		// (get) Token: 0x060006D8 RID: 1752 RVA: 0x00023ECA File Offset: 0x000220CA
		// (set) Token: 0x060006D9 RID: 1753 RVA: 0x00023ED2 File Offset: 0x000220D2
		public byte Station { get; set; }

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x060006DA RID: 1754 RVA: 0x00023EDC File Offset: 0x000220DC
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = PanasonicHelper.BuildReadCommand(station, address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = PanasonicHelper.ExtraActualData(operateResult2.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x060006DB RID: 1755 RVA: 0x00023F4C File Offset: 0x0002214C
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = PanasonicHelper.BuildWriteCommand(station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = PanasonicHelper.ExtraActualData(operateResult2.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x060006DC RID: 1756 RVA: 0x00023FBC File Offset: 0x000221BC
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<string, int> operateResult = PanasonicHelper.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = PanasonicHelper.BuildReadCommand(station, address, length);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = base.ReadBase(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						OperateResult<byte[]> operateResult4 = PanasonicHelper.ExtraActualData(operateResult3.Content);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
						}
						else
						{
							result = OperateResult.CreateSuccessResult<bool[]>(SoftBasic.ByteToBoolArray(operateResult4.Content).SelectMiddle(operateResult.Content2 % 16, (int)length));
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.ReadBool(System.String)" />
		// Token: 0x060006DD RID: 1757 RVA: 0x00024098 File Offset: 0x00022298
		[HslMqttApi("ReadBool", "")]
		public override OperateResult<bool> ReadBool(string address)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = PanasonicHelper.BuildReadOneCoil(station, address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool>(operateResult2);
				}
				else
				{
					result = PanasonicHelper.ExtraActualBool(operateResult2.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.Write(System.String,System.Boolean[])" />
		// Token: 0x060006DE RID: 1758 RVA: 0x00024110 File Offset: 0x00022310
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] values)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<string, int> operateResult = PanasonicHelper.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content2 % 16 != 0;
				if (flag2)
				{
					result = new OperateResult(StringResources.Language.PanasonicAddressBitStartMulti16);
				}
				else
				{
					bool flag3 = values.Length % 16 != 0;
					if (flag3)
					{
						result = new OperateResult(StringResources.Language.PanasonicBoolLengthMulti16);
					}
					else
					{
						byte[] values2 = SoftBasic.BoolArrayToByte(values);
						OperateResult<byte[]> operateResult2 = PanasonicHelper.BuildWriteCommand(station, address, values2);
						bool flag4 = !operateResult2.IsSuccess;
						if (flag4)
						{
							result = operateResult2;
						}
						else
						{
							OperateResult<byte[]> operateResult3 = base.ReadBase(operateResult2.Content);
							bool flag5 = !operateResult3.IsSuccess;
							if (flag5)
							{
								result = operateResult3;
							}
							else
							{
								result = PanasonicHelper.ExtraActualData(operateResult3.Content);
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocolOverTcp.Write(System.String,System.Boolean)" />
		// Token: 0x060006DF RID: 1759 RVA: 0x000241F8 File Offset: 0x000223F8
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = PanasonicHelper.BuildWriteOneCoil(station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = PanasonicHelper.ExtraActualData(operateResult2.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocol.ReadBool(System.String)" />
		// Token: 0x060006E0 RID: 1760 RVA: 0x00024268 File Offset: 0x00022468
		[DebuggerStepThrough]
		public override Task<OperateResult<bool>> ReadBoolAsync(string address)
		{
			PanasonicMewtocol.<ReadBoolAsync>d__11 <ReadBoolAsync>d__ = new PanasonicMewtocol.<ReadBoolAsync>d__11();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<PanasonicMewtocol.<ReadBoolAsync>d__11>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Panasonic.PanasonicMewtocol.Write(System.String,System.Boolean)" />
		// Token: 0x060006E1 RID: 1761 RVA: 0x000242B4 File Offset: 0x000224B4
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			PanasonicMewtocol.<WriteAsync>d__12 <WriteAsync>d__ = new PanasonicMewtocol.<WriteAsync>d__12();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<PanasonicMewtocol.<WriteAsync>d__12>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x060006E2 RID: 1762 RVA: 0x00024306 File Offset: 0x00022506
		public override string ToString()
		{
			return string.Format("Panasonic Mewtocol[{0}:{1}]", base.PortName, base.BaudRate);
		}
	}
}
